//
//  File.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/6.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import Foundation

extension LGTPostDetailVC{
    
    
    func initPreViewFooterView(){
        let previewBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "modify", title: "修改", color:TextColor, font: Font_Normal_18, backColor: UIColor.white)
        self.footerView!.addSubview(previewBtn)
        previewBtn.snp.makeConstraints { (make) in
            make.bottom.left.equalTo(0)
            make.height.equalTo(TabBarH())
            make.width.equalTo(SizeFit(116))
        }
        
        let postBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "applicationforissuance", title: "申请发布", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
        self.footerView!.addSubview(postBtn)
        
        postBtn.snp.makeConstraints { (make) in
            make.bottom.right.equalTo(0)
            make.left.equalTo(previewBtn.snp_rightMargin)
            make.height.equalTo(TabBarH())
        }
        
        previewBtn.tag = LGTRWZDetailBtnType.ModifyBtnType.rawValue
        postBtn.tag = LGTRWZDetailBtnType.PublicBtnType.rawValue
        previewBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
        postBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
    }
    
    func initRWZTaskDetailFooterView(){
        let buttonW = KScreenW / 3.0 - CGFloat(1)
        let buttonH = TabBarH()
        let statues = self.topParam!["status"] as! Int
        //2未通过 1审核中  无按钮
        if statues == 1 || statues == 2{
            self.tableView?.frame = CGRect.init(x: 0.0, y: NavBarH(), width: Double(KScreenW), height: Double(KScreenH) - NavBarH())
            self.footerView?.isHidden = true
            return
        }
        
        //5 已撤回 申请发布
        if statues == 5{
//            let postBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "applicationforissuance", title: "查看单价", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            let postBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: "查看单价", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(postBtn)
            postBtn.snp.makeConstraints { (make) in
                make.bottom.right.equalTo(0)
                make.left.equalTo(0)
                make.height.equalTo(TabBarH())
            }
            postBtn.tag = LGTRWZDetailBtnType.LookBtnType.rawValue
            postBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            return
        }
        
        // 7 已完成 6 已过期 8 已下架
        if statues == 7 ||  statues == 8 || statues == 6{
            //查看单价  以此为模版继续发布
//            let lookBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "查看单价", font:Font_Normal_18, color: TextColor, selectedColor: TextColor, backColor: UIColor.white)
            let lookBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "查看单价", font:Font_Normal_18, color: UIColor.white, selectedColor: UIColor.white, backColor: YellowColor)
            lookBtn.tag = LGTRWZDetailBtnType.LookBtnType.rawValue
            self.footerView!.addSubview(lookBtn)
            lookBtn.snp.makeConstraints { (make) in
                make.top.left.equalTo(0)
                make.width.equalTo(KScreenW)
                make.height.equalTo(buttonH)
            }
           lookBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            
            
//            //以此为模板继续发布
//            let postBtn2:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName:"applicationforissuance", title: "以此为模板继续发布", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
//            self.footerView!.addSubview(postBtn2)
//            postBtn2.snp.makeConstraints { (make) in
//                make.bottom.right.equalTo(0)
//                make.left.equalTo(lookBtn.snp_rightMargin).offset(1)
//                make.height.equalTo(TabBarH())
//            }
//            postBtn2.isHidden = true
//
//            lookBtn.tag = LGTRWZDetailBtnType.LookBtnType.rawValue
//            postBtn2.tag = LGTRWZDetailBtnType.RePublicBtnType.rawValue
//            lookBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
//            postBtn2.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            return
        }
        
        
        //4 暂停中： 撤回 继续 查看单价
        if statues == 4{
            //撤回 继续 查看单价
            let rebackBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "withdraw", title: "撤回", color:TextColor, font: Font_Normal_18, backColor: UIColor.white)
            self.footerView!.addSubview(rebackBtn)
            rebackBtn.snp.makeConstraints { (make) in
                make.top.equalTo(0)
                make.left.equalTo(0)
                make.width.equalTo(buttonW)
                make.height.equalTo(TabBarH())
            }
            
            //go_on 继续
            let goonBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "go_on", title: "继续", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(goonBtn)
            goonBtn.snp.makeConstraints { (make) in
                make.top.equalTo(0)
                make.left.equalTo(rebackBtn.snp_rightMargin).offset(10)
                //make.width.equalTo(buttonW)
                make.width.equalTo(goonBtn)
                make.height.equalTo(TabBarH())
            }
            
            let lookBtn:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "查看单价", font:Font_Normal_18, color: UIColor.white, selectedColor: UIColor.white, backColor: YellowColor)
            self.footerView!.addSubview(lookBtn)
            lookBtn.snp.makeConstraints { (make) in
                make.top.equalTo(0)
                make.left.equalTo(goonBtn.snp_rightMargin).offset(10)
                make.right.equalTo(0)
                make.width.equalTo(goonBtn)
                make.height.equalTo(buttonH)
            }
            
            rebackBtn.tag = LGTRWZDetailBtnType.RebackBtnType.rawValue
            goonBtn.tag = LGTRWZDetailBtnType.GoOnBtnType.rawValue
            lookBtn.tag = LGTRWZDetailBtnType.LookBtnType.rawValue
            goonBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            rebackBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            lookBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            return
        }
        
        // 3 派发中： 更多 暂停 追加
        if statues == 3{
            //更多 暂停 追加
            let moreBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "more", title: "更多", color:TextColor, font: Font_Normal_18, backColor: UIColor.white)
            self.footerView!.addSubview(moreBtn)
            moreBtn.snp.makeConstraints { (make) in
                make.top.equalTo(0)
                make.left.equalTo(0)
                make.width.equalTo(buttonW)
                make.height.equalTo(TabBarH())
            }
            
            let stopBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "stop", title: "暂停", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(stopBtn)
            stopBtn.snp.makeConstraints { (make) in
                make.top.equalTo(0)
                make.left.equalTo(moreBtn.snp_rightMargin).offset(10)
                //make.width.equalTo(buttonW)
                make.width.equalTo(moreBtn)
                make.height.equalTo(TabBarH())
            }
            
            let addBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "append", title: "追加", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(addBtn)
            addBtn.snp.makeConstraints { (make) in
                make.top.equalTo(0)
                make.left.equalTo(stopBtn.snp_rightMargin).offset(10)
                make.right.equalTo(0)
                make.width.equalTo(stopBtn)
                make.height.equalTo(buttonH)
            }
            
            moreBtn.tag = LGTRWZDetailBtnType.MoreBtnType.rawValue
            stopBtn.tag = LGTRWZDetailBtnType.StopBtnType.rawValue
            addBtn.tag = LGTRWZDetailBtnType.AddBtnType.rawValue
            moreBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            stopBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            addBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
        }
    }
    
    func initLLZTaskDetailFooterView(){
          //没有申请过任务 显示申请按钮
            let postBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: "立即申请", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(postBtn)
            postBtn.snp.makeConstraints { (make) in
                make.bottom.right.equalTo(0)
                make.left.equalTo(0)
                make.height.equalTo(TabBarH())
            }
            postBtn.tag = LGTRWZDetailBtnType.TaskApplyType.rawValue
            postBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
    }

    func initLLZOrderFooterView(){
        var isFullView:Bool = true
        let status:Int = self.taskDetailInfo["status"] as? Int ?? 0
        var tabarH = TabBarH()
        
        //申诉未通过  申请介入 10 11
        if status == 10{
        //if status == 10 || status == 11{
            let submitBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: "申请介入", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(submitBtn)
            submitBtn.tag = LGTRWZDetailBtnType.ApplyGotoType.rawValue
            submitBtn.snp.makeConstraints { (make) in
                make.bottom.right.equalTo(0)
                make.left.equalTo(0)
                make.height.equalTo(TabBarH())
            }
            isFullView = false
            self.createTimer(btn: submitBtn)
            submitBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
        }
        
        //审核未通过 申请申诉 6 7
        if status == 6{
        //if status == 6 || status == 7{
            let submitBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: "申请申诉", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(submitBtn)
            submitBtn.tag = LGTRWZDetailBtnType.ApplyBegType.rawValue
            submitBtn.snp.makeConstraints { (make) in
                make.bottom.right.equalTo(0)
                make.left.equalTo(0)
                make.height.equalTo(TabBarH())
            }
            isFullView = false
            self.createTimer(btn: submitBtn)
            submitBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
        }
        
        //等待提交   放弃 和 提交 1
        if status == 1{
            let giveupBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "modify", title: "放弃", color:TextColor, font: Font_Normal_18, backColor: UIColor.white)
            self.footerView!.addSubview(giveupBtn)
            giveupBtn.snp.makeConstraints { (make) in
                make.bottom.equalTo(0)
                make.left.equalTo(0)
                make.width.equalTo(KScreenW/3)
                make.height.equalTo(TabBarH())
            }
            giveupBtn.tag = LGTRWZDetailBtnType.OrderGiveUPType.rawValue
            giveupBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            
            let submitBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: "提交", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(submitBtn)
            submitBtn.snp.makeConstraints { (make) in
                make.bottom.right.equalTo(0)
                make.left.equalTo(giveupBtn.snp_rightMargin)
                make.height.equalTo(TabBarH())
            }
            submitBtn.tag = LGTRWZDetailBtnType.OrderSubmitType.rawValue
            submitBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            isFullView = false
            self.createTimer(btn: submitBtn)
        }
        
        if isFullView == true{
            tabarH = 0
            self.footerView?.isHidden = true
        }else{
            self.footerView?.isHidden = false
        }
        
        self.tableView?.frame =  CGRect.init(x: 0.0, y: NavBarH(), width: Double(KScreenW), height: Double(KScreenH) - NavBarH() - tabarH)
    }

    func initRWZOrderViewType(){
        var isFullView:Bool = true
        let status:Int = self.taskDetailInfo["status"] as? Int ?? 0
        var tabarH = TabBarH()
        
        //申诉待处理8   待审核4 //拒绝 通过
        if status == 4 || status == 8{
            
            
//            let giveupBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "refuse", title: "拒绝", color:TextColor, font: Font_Normal_18, backColor: UIColor.white)
//            self.footerView!.addSubview(giveupBtn)
//            giveupBtn.snp.makeConstraints { (make) in
//                make.bottom.equalTo(0)
//                make.left.equalTo(0)
//                make.width.equalTo(KScreenW/3)
//                make.height.equalTo(TabBarH())
//            }
//            giveupBtn.tag = LGTRWZDetailBtnType.RWZRefuseType.rawValue
//            giveupBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
//
//            let submitBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "adopt", title: "通过", color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
//            self.footerView!.addSubview(submitBtn)
//            submitBtn.snp.makeConstraints { (make) in
//                make.bottom.right.equalTo(0)
//                make.left.equalTo(giveupBtn.snp_rightMargin)
//                make.height.equalTo(TabBarH())
//            }
//            submitBtn.tag = LGTRWZDetailBtnType.RWZPassType.rawValue
//            submitBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            
            // 4待审核
            var title:String = "去审核"
            var tag:Int = LGTRWZDetailBtnType.RWZGotoReview.rawValue
            if status == 8{
                title = "去处理"
                 tag = LGTRWZDetailBtnType.RWZGotoDeal.rawValue
            }
            let submitBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: title, color:UIColor.white, font: Font_Normal_18, backColor: YellowColor)
            self.footerView!.addSubview(submitBtn)
            submitBtn.snp.makeConstraints { (make) in
                make.bottom.right.equalTo(0)
                make.left.equalTo(0)
                make.height.equalTo(TabBarH())
            }
            submitBtn.tag = tag
            submitBtn.addTarget(self, action: #selector(postAction), for: UIControl.Event.touchUpInside)
            isFullView = false
        }
        
       if isFullView == true{
            tabarH = 0
            self.footerView?.isHidden = true
        }else{
            self.footerView?.isHidden = false
        }
        
        self.tableView?.frame =  CGRect.init(x: 0.0, y: NavBarH(), width: Double(KScreenW), height: Double(KScreenH) - NavBarH() - tabarH)
    }
    
    func createTimer(btn:UIButton){
        self.timerBtn = btn
        let timeStr:String = self.taskDetailInfo["expire_time"] as? String ?? ""
        let expireTimeDate:Date = BaseUtil.stringConvertDate(string: timeStr)
        let currentDate:Date = Date()
        if expireTimeDate.timeIntervalSince1970 < currentDate.timeIntervalSince1970{
            return
        }
        
        weak var weakSelf = self
        self.timer = Timer.init(timeInterval: 1, repeats: true, block: { (timer) in
            weakSelf?.timerRun()
        })
        RunLoop.main.add(self.timer!, forMode: RunLoop.Mode.common)
    }
    
    func timerRun(){
        let timeStr:String = self.taskDetailInfo["expire_time"] as? String ?? ""
        let status:Int = self.taskDetailInfo["status"] as? Int ?? 0
        let expireTimeDate:Date = BaseUtil.stringConvertDate(string: timeStr)
        let currentDate:Date = Date()
        if expireTimeDate.timeIntervalSince1970 < currentDate.timeIntervalSince1970{
           self.timer?.invalidate()
           self.timer = nil
        }
        
        //申请申诉（剩余时间：）
        //申请介入（剩余时间：）
        //剩余时间：提交
        
        let timeDis = expireTimeDate.timeIntervalSince1970 - currentDate.timeIntervalSince1970
        let hour:Int = Int(timeDis / 3600)
        let minute:Int = (Int(timeDis) - 3600 * hour) / 60
        let second:Int = (Int(timeDis) - 3600 * hour - minute * 60)
        let timeLeastStr:String = String.init(format: "%02ld:%02ld:%02ld", hour, minute, second)
        var btnStr:String = ""
        //待提交
        if status == 1{
            btnStr = String.init(format: "剩余时间：%@提交", timeLeastStr)
        }else if status == 10 || status == 11{
             btnStr = String.init(format: "申请介入（剩余时间：%@）", timeLeastStr)
        }else{
            btnStr = String.init(format: "申请申诉（剩余时间：%@）", timeLeastStr)
        }
        
        self.timerBtn?.setTitle(btnStr, for: UIControl.State.normal)
        self.timerBtn?.setTitle(btnStr, for: UIControl.State.selected)
        
    }
}
